﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

using StudentModule.Models;
using StudentModule.Services;
using ServiceAgent;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Events;
using System.Windows.Input;
using System.Windows;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using StudentModule.Views;
using ServiceAgent.StudentService;
using System.Collections.Generic;
using ServiceAgent.CatalogueService;
using StudentModule.Events;

namespace StudentModule.ViewModels
{
    /// <summary>
    /// ViewModel for NhapThongTinView.
    /// </summary>
    public class NhapThongTinViewModel : NotificationObject, INavigationAware
    {
        #region Fields
        private IRegionNavigationService navigationService;

        private IEventAggregator eventAggregator;
        private readonly IUnityContainer container;
        private IRegionManager regionManager;

        int uid;
        SinhVienDetailDTO sinhVien;

        ObservableCollection<string> xepLoaiHocLucList;

        ObservableCollection<DonViDTO> donViList;
        ObservableCollection<LopDTO> lopList;
        ObservableCollection<DanTocDTO> danTocList;
        ObservableCollection<TonGiaoDTO> tonGiaoList;
        ObservableCollection<ServiceAgent.CatalogueService.ChinhSachDTO> chinhSachList;
        ObservableCollection<DVHCCap1DTO> dvhcCap1List;
        ObservableCollection<DVHCCap2DTO> dvhcCap2ThuongTruList;
        ObservableCollection<DVHCCap2DTO> dvhcCap2TamTruList;

        DVHCCap1DTO thuongTru;
        DVHCCap1DTO tamTru;

        DonViDTO currentDonVi;

        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public NhapThongTinViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container)
        {
            this.eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.container = container;
        }
        #endregion

        #region Administrative Properties
        public ObservableCollection<string> XepLoaiHocLucList
        {
            get { return xepLoaiHocLucList; }
            set
            {
                xepLoaiHocLucList = value;
                RaisePropertyChanged(() => this.XepLoaiHocLucList);
            }
        }

        public ServiceAgent.StudentService.SinhVienDetailDTO SinhVien
        {
            get { return sinhVien; }
            set
            {
                sinhVien = value;
                RaisePropertyChanged(() => this.SinhVien);
            }
        }

        public ServiceAgent.CatalogueService.DonViDTO CurrentDonVi
        {
            get { return currentDonVi; }
            set
            {
                currentDonVi = value;
                RaisePropertyChanged(() => this.CurrentDonVi);
                this.GetLopListByDonViChange(CurrentDonVi.ID);
            }
        }

        public ObservableCollection<DonViDTO> DonViList
        {
            get { return donViList; }
            set
            {
                donViList = value;
                RaisePropertyChanged(() => this.DonViList);
            }
        }

        public ObservableCollection<LopDTO> LopList
        {
            get { return lopList; }
            set
            {
                lopList = value;
                RaisePropertyChanged(() => this.LopList);
            }
        }

        public ObservableCollection<DanTocDTO> DanTocList
        {
            get { return danTocList; }
            set
            {
                danTocList = value;
                RaisePropertyChanged(() => this.DanTocList);
            }
        }

        public ObservableCollection<TonGiaoDTO> TonGiaoList
        {
            get { return tonGiaoList; }
            set
            {
                tonGiaoList = value;
                RaisePropertyChanged(() => this.TonGiaoList);
            }
        }

        public ObservableCollection<ServiceAgent.CatalogueService.ChinhSachDTO> ChinhSachList
        {
            get { return chinhSachList; }
            set
            {
                chinhSachList = value;
                RaisePropertyChanged(() => this.ChinhSachList);
            }
        }

        public ObservableCollection<DVHCCap1DTO> DVHCCap1List
        {
            get { return dvhcCap1List; }
            set
            {
                dvhcCap1List = value;
                RaisePropertyChanged(() => this.DVHCCap1List);
            }
        }

        public ObservableCollection<DVHCCap2DTO> DVHCCap2ThuongTruList
        {
            get { return dvhcCap2ThuongTruList; }
            set
            {
                dvhcCap2ThuongTruList = value;
                RaisePropertyChanged(() => this.DVHCCap2ThuongTruList);
            }
        }
        public ObservableCollection<DVHCCap2DTO> DVHCCap2TamTruList
        {
            get { return dvhcCap2TamTruList; }
            set
            {
                dvhcCap2TamTruList = value;
                RaisePropertyChanged(() => this.DVHCCap2TamTruList);
            }
        }

        public ServiceAgent.CatalogueService.DVHCCap1DTO ThuongTru
        {
            get { return thuongTru; }
            set
            {
                thuongTru = value;
                RaisePropertyChanged(() => this.ThuongTru);
                GetDVHCCap2ThuongTruByDVHCCap1Change();
            }
        }

        public ServiceAgent.CatalogueService.DVHCCap1DTO TamTru
        {
            get { return tamTru; }
            set
            {
                tamTru = value;
                RaisePropertyChanged(() => this.TamTru);
                GetDVHCCap2TamTruByDVHCCap1Change();
            }
        }

        #endregion

        #region Command Properties
        public ICommand SaveCommand { get; private set; }
        public DelegateCommand<object> GoBackCommand { get; private set; }

        #endregion

        #region Private Methods
        private void Initialize()
        {
            this.XepLoaiHocLucList = Lists.HocLucList;

            this.SaveCommand = new DelegateCommand(this.OnRaiseSave);
            this.GoBackCommand = new DelegateCommand<object>(GoBack, CanGoBack);

        }

        #region SinhVien Info Methods
        void GetSinhVien(int id)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien = proxy.GetSinhVienDetailByID(id);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        #endregion

        void GetDonViList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DonViList = proxy.GetDonViListByCapDonVi();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetLopListByDonVi(int ID)
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    LopList = proxy.GetLopByDonViID(SinhVien.DonViID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetLopListByDonViChange(int ID)
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    LopList = proxy.GetLopByDonViID(CurrentDonVi.ID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetTonGiaoList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    TonGiaoList = proxy.GetTonGiaoList();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanTocList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DanTocList = proxy.GetDanTocList();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetChinhSachList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    ChinhSachList = proxy.GetChinhSachList();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDVHCCap1List()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DVHCCap1List = proxy.GetDVHCCap1List();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDVHCCap2ThuongTruByDVHCCap1()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DVHCCap2ThuongTruList = proxy.GetDVHCCap2ListByDVHCCap1ID(SinhVien.LienLac.DiaChiThuongTru.TinhThanhID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDVHCCap2ThuongTruByDVHCCap1Change()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DVHCCap2ThuongTruList = proxy.GetDVHCCap2ListByDVHCCap1ID(ThuongTru.ID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDVHCCap2TamTruByDVHCCap1()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DVHCCap2TamTruList = proxy.GetDVHCCap2ListByDVHCCap1ID(SinhVien.LienLac.DiaChiThuongTru.TinhThanhID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDVHCCap2TamTruByDVHCCap1Change()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DVHCCap2TamTruList = proxy.GetDVHCCap2ListByDVHCCap1ID(TamTru.ID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSave()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.SaveSinhVien(SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception) { MessageBox.Show("Có lỗi xảy ra"); }
        }

        private void GoBack(object commandArg)
        {
            if (navigationService.Journal.CanGoBack)
            {
                navigationService.Journal.GoBack();
            }
        }

        private bool CanGoBack(object commandArg)
        {
            return navigationService.Journal.CanGoBack;
        }
        #endregion

        #region INavigationAware Members
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // Called to see if this view can handle the navigation request.
            // If it can, this view is activated.
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

            // Called when this view is deactivated as a result of navigation to another view.
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //// Called to initialize this view during navigation.

            // getting a reference to navigation service:
            if (navigationService == null)
                navigationService = navigationContext.NavigationService;

            //// Retrieve any required paramaters from the navigation Uri.
            string id = navigationContext.Parameters["ID"];
            if (!string.IsNullOrEmpty(id))
            {
                this.uid = Convert.ToInt32(id);
                this.GetSinhVien(uid);
                this.GetChinhSachList();
                this.GetDanTocList();
                this.GetTonGiaoList();
                this.GetDonViList();
                this.GetLopListByDonVi(SinhVien.DonViID);

                this.GetDVHCCap1List();
                this.GetDVHCCap2TamTruByDVHCCap1();
                this.GetDVHCCap2ThuongTruByDVHCCap1();

                this.Initialize();
            }
        }
        #endregion
    }
}
